﻿using HRMSystem.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HRMSystem.DBServices
{
    public  class CompanyService : GenericService<Company>
    {
       
       
        public static List<CompanyDetails> findCompanyDetails(int managerId)
        {
            try
            {
             var query = (from c in db.Companies
                       join b in db.Branches on c.ID equals b.COMPANY_ID
                       join d in db.Departments on b.ID equals d.BRANCH_ID
                       join de in db.Titles on d.ID equals de.DEPARTMENT_ID
                       join e in db.Employees on de.EMPLOYEE_ID equals e.ID
                       where e.ID == managerId
                       select new CompanyDetails()
                       {
                           CID = c.ID,
                           NAME = c.NAME,
                           REG_NO = c.REG_NO,
                           START_DATE = c.START_DATE,
                           VISION = c.VISION,
                           MISSION = c.MISSION,
                           YOURADDRESS = b.ADDRESS,
                           DEPARTMENTID = d.ID,
                           DEPARTMENTNAME = d.NAME

                       }).ToList();
             return query;
            }


            catch (Exception)
            {
                return null;
            }
        }

        public static List<CompanyDetails> findHeadOffice(int CId)
        {
            try
            {
                var query = (from c in db.Companies
                             join b in db.Branches on c.ID equals b.COMPANY_ID
                             where c.ID == CId &&
                             b.TYPE == "Head Office"

                             select new CompanyDetails()
                             {
                                 HOADDRESS = b.ADDRESS
                             }).ToList();
                return query;
            }
            catch (Exception)
            {
                return null;
            }
        }

        internal static void UpdateHOAddress(int CId, String p)
        {
            try
            {
                var query = from c in db.Companies
                            join b in db.Branches on c.ID equals b.COMPANY_ID
                            where c.ID == CId &&
                            b.TYPE == "Head Office"
                            select new { b};
                           
                foreach (var add in query)
                {
                    add.b.ADDRESS = p;
                }
                db.SaveChanges();  

            }
            catch (Exception e)
            {
                throw new NotImplementedException();
            }

        }

        internal static void updateCompanyDetails(CompanyDetails editDetails, int managerId)
        {
            try
            {
                var query = from c in db.Companies
                           join b in db.Branches on c.ID equals b.COMPANY_ID
                           join d in db.Departments on b.ID equals d.BRANCH_ID
                           join de in db.Titles on d.ID equals de.DEPARTMENT_ID
                            join e in db.Employees on de.EMPLOYEE_ID equals e.ID
                            where e.ID == managerId
                           select new { c, b, d };

                foreach (var add in query)
                {
                    add.c.NAME = editDetails.NAME;
                    add.c.REG_NO = editDetails.REG_NO;
                    add.c.START_DATE = editDetails.START_DATE;
                    add.c.VISION = editDetails.VISION;
                    add.c.MISSION = editDetails.MISSION;
                    add.b.ADDRESS = editDetails.YOURADDRESS;
                    add.d.NAME = editDetails.DEPARTMENTNAME;
                }
                db.SaveChanges();

            }
            catch (Exception e)
            {
                throw new NotImplementedException();
            }
        }

        internal static void insertCompamy(string name, string short_name, string reg_no, DateTime start_date, string vision, string mission)
        {
            try
            {
                var query = from com in db.Companies
                            select new { com };

                foreach (var add in query)
                {
                    add.com.NAME = name;
                    add.com.SHORT_NAME = short_name; 
                    add.com.REG_NO = reg_no;
                    add.com.START_DATE = start_date;
                    add.com.VISION = vision;
                    add.com.MISSION = mission;
                }
                db.SaveChanges();


            }

            catch (Exception e)
            {
                throw new NotImplementedException();
            }
        }



        internal static List<Company> GetAllCompanies()
        {
            try
            {
                return db.Companies.Where(c => c.ISDELETE == false).ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        internal static Company GetCompanyByCompanyId(int companyId)
        {
            try
            {
                return db.Companies.SingleOrDefault(c => c.ID == companyId && c.ISDELETE == false);
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
